package com.cxl.price.service;

import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;

import org.htmlparser.Node;
import org.htmlparser.Tag;
import org.htmlparser.util.NodeList;
import org.springframework.stereotype.Service;

import com.cxl.parser.Grab360Buy;
import com.cxl.price.bean.Ptype;
import com.cxl.price.dao.PtypeDao;
import com.summer.core.service.impl.BaseServiceImpl;

/**
 * @about: 产品类型 service 实现
 * @author: wwwlike
 * @create: 2011-06-23
 */
@Service
public class PtypeServiceImpl extends BaseServiceImpl<Ptype, String, PtypeDao>
		implements PtypeService {

	@Resource
	public void setDao(PtypeDao dao) {
		this.dao = dao;
	}
	
	
	
	public void addType() throws UnsupportedEncodingException
	{
		
		String url="http://www.360buy.com/allSort.aspx";
		Grab360Buy grab=new Grab360Buy();
		NodeList listNode=grab.getNodesByFilter(url,grab.getPtypeFirstFilter(),-1) ;//第一层
		int i=0;
		while(listNode.size()>0)
		{
			Node node=listNode.elementAt(0);
			Node nodeFirst=grab.getNextNodeListByFilter(node,grab.getPtypeOneFilter()).elementAt(0); //第1层结果
			
			String name =new String(nodeFirst.toPlainTextString()
							.getBytes(grab.encode)).toString();
			Tag a=(Tag)nodeFirst;
			Ptype ptype=new Ptype();
			ptype.setName(name);
			ptype.setUrl(a.getAttribute("href"));
			dao.save(ptype);
			NodeList nodeSecondList=grab.getNextNodeListByFilter(node,grab.getPtypeSecondFilter());//第2层 
		
			
			while(nodeSecondList.size()>0)
			{
				Node node1=nodeSecondList.elementAt(0);
				
				Node nodeSecond=grab.getNextNodeListByFilter(node1,grab.getPtypeTwoFilter()).elementAt(0); //第2层结果
				Ptype ptype2=new Ptype();
				if(nodeSecond==null)
				{
					nodeSecond=grab.getNextNodeListByFilter(node1,grab.getPtypeTwoFilter1()).elementAt(0); //第2层结果
				}
				else
				{   
					Tag b=(Tag)nodeSecond;
					ptype2.setUrl(b.getAttribute("href"));
				}
				 name =new String(nodeSecond.toPlainTextString()
						.getBytes(grab.encode)).toString();
				
				ptype2.setName(name);
				ptype2.setPtype(ptype);
				dao.save(ptype2);
				
				
				NodeList nodethirdList=grab.getNextNodeListByFilter(node1,grab.getPtypeThirdFilter());//第3层 
					while(nodethirdList.size()>0)
					{
						Node node2=nodethirdList.elementAt(0);
						NodeList lastList=grab.getNextNodeListByFilter(node2,grab.getPtypeThreeFilter());
						while(lastList.size()>0)
						{
							Node node3=lastList.elementAt(0);	
							
							name =new String(node3.toPlainTextString()
									.getBytes(grab.encode)).toString();
							Tag c = (Tag) node3;
							
							Ptype ptype3=new Ptype();
							ptype3.setName(name);
							String href=c.getAttribute("href");
							
							if(href.indexOf("360buy")==-1&& href.startsWith("/")==false)
							{
								href="http://www.360buy.com/"+href;
							}
							else if(href.indexOf("360buy")==-1&& href.startsWith("/"))
							{
								href="http://www.360buy.com"+href;
							}
							
							ptype3.setUrl(href);
							ptype3.setPtype(ptype2);
							dao.save(ptype3);
							
						//	System.out.println(a.getAttribute("href"));
							//System.out.println(node3);
							lastList.remove(0);
						}
						nodethirdList.remove(0);
						i++;
					}
				
				nodeSecondList.remove(0);
			}
		
			listNode.remove(0);
		}
		
		System.out.println(i);
	}

}
